<template>
  <div class="app-container">
    <div class="filter-container">
      <div class="rolePage">
        <el-row>
          <el-col :sm="4">
            <div class="g-title">成本统计</div>
          </el-col>
          <el-col :sm="20" style="text-align: right">
            <span class="role-add" @click="handleCreate()"><i class="el-icon-plus" />添加</span>
          </el-col>
        </el-row>

        <el-date-picker
          v-model="dateRange"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          class="filter-item"
          @change="fetchData()"
        />
      </div>
    </div>
    <el-row class="table">
      <el-table
        ref="multipleTable"
        :data="list"
        tooltip-effect="dark"
        border
        style="width: 100%"
        @selection-change="handleSelectionChange"
      >
        <el-table-column prop="costsDate" label="日期" show-overflow-tooltip align="center" />
        <el-table-column
          prop="dayInput"
          label="每日投入"
          show-overflow-tooltip
          align="center"
        />
        <el-table-column
          prop="dayStorage"
          label="每日入库"
          show-overflow-tooltip
          align="center"
        />
        <el-table-column
          prop="lostMaterialsCost"
          label="损耗物料费用"
          show-overflow-tooltip
          align="center"
        />
        <el-table-column prop="depSalary" label="运营部工资" show-overflow-tooltip align="center" />
        <el-table-column prop="utilityBills" label="水电费用" show-overflow-tooltip align="center" />
        <el-table-column
          prop="eqDepreciation"
          label="设备折旧费用"
          show-overflow-tooltip
          align="center"
        />
        <el-table-column
          prop="eqSpareBills"
          label="设备备品备件费用"
          show-overflow-tooltip
          align="center"
        />
        <el-table-column
          prop="facSpareBills"
          label="厂务备品备件费用"
          show-overflow-tooltip
          align="center"
        />
        <el-table-column prop="laborBills" label="劳保五金费用" show-overflow-tooltip align="center" />
        <el-table-column prop="invitBills" label="招待费用" show-overflow-tooltip align="center" />
        <el-table-column prop="dayTotalBills" label="每日合计费用" show-overflow-tooltip align="center" />
        <el-table-column prop="singleCosts" label="单瓦成本" show-overflow-tooltip align="center" />
        <el-table-column prop="inputCosts" label="投入计算单瓦成本" show-overflow-tooltip align="center" />
        <el-table-column prop="remarks" label="备注" show-overflow-tooltip align="center" />
        <el-table-column label="操作" align="center" width="240">
          <template slot-scope="{row,$index}">
            <el-button type="primary" size="mini" @click="handleUpdate(row)">
              修改
            </el-button>
            <el-button
              v-if="row.status != 'deleted'"
              size="mini"
              type="danger"
              @click="handleDelete(row, $index)"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-row class="pagination">
        <el-col>
          <el-pagination
            background
            layout="total, sizes, prev, pager, next, jumper"
            :page-size="pageSize"
            :total="total"
            :current-page="pageNo"
            @current-change="fetchData"
            @size-change="pageSizeChange"
          />
        </el-col>
      </el-row>
    </el-row>
    <!-- 查看详情弹窗 -->
    <el-dialog title="查看" :visible.sync="dialogSee">
      <el-form
        ref="SeeForm"
        :model="SeeData"
        label-position="left"
        label-width="80px"
        style="width: 400px; margin-left:50px;"
      >
        <el-form-item label="投入达成">
          <span>{{ SeeData.inputFinish }}</span>
        </el-form-item>
        <el-form-item label="碎片">
          <span>{{ SeeData.remarkSuiPian }}</span>
        </el-form-item>
        <el-form-item label="良率">
          <span>{{ SeeData.remarkGoodRates }}</span>
        </el-form-item>
        <el-form-item label="其他">
          <span>{{ SeeData.remarkOther }}</span>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogSee = false">
          关闭
        </el-button>
      </div>
    </el-dialog>
    <!-- 添加修改弹窗 -->
    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form
        ref="dataForm"
        :rules="rules"
        :model="temp"
        label-position="left"
        label-width="140px"
        style="width: 400px; margin-left:50px;"
      >
        <el-form-item label="日期" prop="costsDate">
          <el-date-picker
            v-model="temp.costsDate"
            type="date"
            placeholder="选择日期"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd"
          />
        </el-form-item>
        <el-form-item label="每日投入" prop="dayInput">
          <el-input
            v-model="temp.dayInput"
            placeholder="请输入内容"
            type="text"
            oninput="value=value.replace(/[^\d.]/g,'')"
            clearable
            @change="inputChange()"
          />
        </el-form-item>
        <el-form-item label="每日入库" prop="dayStorage">
          <el-input
            v-model="temp.dayStorage"
            placeholder="请输入内容"
            clearable
            type="text"
            oninput="value=value.replace(/[^\d.]/g,'')"
            @change="inputChange"
          />
        </el-form-item>
        <el-form-item label="损耗物料费用" prop="lostMaterialsCost">
          <el-input
            v-model="temp.lostMaterialsCost"
            placeholder="请输入内容"
            clearable
            type="text"
            oninput="value=value.replace(/[^-?\d+(\.\d+)?$]/g,'')"
            @change="inputChange"
          />
        </el-form-item>
        <el-form-item label="运营部工资" prop="depSalary">
          <el-input
            v-model="temp.depSalary"
            placeholder="请输入内容"
            clearable
            type="text"
            oninput="value=value.replace(/[^\d.]/g,'')"
            @change="inputChange"
          />
        </el-form-item>
        <el-form-item label="水电费用" prop="utilityBills">
          <el-input
            v-model="temp.utilityBills"
            placeholder="请输入内容"
            clearable
            type="text"
            oninput="value=value.replace(/[^\d.]/g,'')"
            @change="inputChange"
          />
        </el-form-item>
        <el-form-item label="设备折旧费用" prop="eqDepreciation">
          <el-input
            v-model="temp.eqDepreciation"
            placeholder="请输入内容"
            clearable
            type="text"
            oninput="value=value.replace(/[^\d.]/g,'')"
            @change="inputChange"
          />
        </el-form-item>
        <el-form-item label="设备备品备件费用" prop="eqSpareBills">
          <el-input
            v-model="temp.eqSpareBills"
            placeholder="请输入内容"
            clearable
            type="text"
            oninput="value=value.replace(/[^\d.]/g,'')"
            @change="inputChange"
          />
        </el-form-item>
        <el-form-item label="厂务备品备件费用" prop="facSpareBills">
          <el-input
            v-model="temp.facSpareBills"
            placeholder="请输入内容"
            clearable
            type="text"
            oninput="value=value.replace(/[^\d.]/g,'')"
            @change="inputChange"
          />
        </el-form-item>
        <el-form-item label="劳保五金费用" prop="laborBills">
          <el-input
            v-model="temp.laborBills"
            placeholder="请输入内容"
            clearable
            type="text"
            oninput="value=value.replace(/[^\d.]/g,'')"
            @change="inputChange"
          />
        </el-form-item>
        <el-form-item label="招待费用" prop="invitBills">
          <el-input
            v-model="temp.invitBills"
            placeholder="请输入内容"
            clearable
            type="text"
            oninput="value=value.replace(/[^\d.]/g,'')"
            @change="inputChange"
          />
        </el-form-item>
        <el-form-item label="每日合计费用" prop="dayTotalBills">
          <el-input
            v-model="temp.dayTotalBills"
            placeholder="请输入内容"
            clearable
            type="text"
            oninput="value=value.replace(/[^\d.]/g,'')"
          />
        </el-form-item>
        <el-form-item label="单瓦成本" prop="singleCosts">
          <el-input
            v-model="temp.singleCosts"
            placeholder="请输入内容"
            clearable
            type="text"
            oninput="value=value.replace(/[^\d.]/g,'')"
          />
        </el-form-item>
        <el-form-item label="投入计算单瓦成本" prop="inputCosts">
          <el-input
            v-model="temp.inputCosts"
            placeholder="请输入内容"
            clearable
            type="text"
            oninput="value=value.replace(/[^\d.]/g,'')"
          />
        </el-form-item>
        <el-form-item label="备注" prop="remarks">
          <el-input v-model="temp.remarks" placeholder="请输入内容" clearable />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">
          关闭
        </el-button>
        <el-button type="primary" @click="dialogStatus === 'create' ? createData() : updateData()">
          {{ textMap[dialogStatus] }}
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import './css/index.scss'
import { costsAdd, costsAllItems, costsDelete, costsUpdate } from '@/api/report'
import { BigNumber } from 'bignumber.js'

export default {
  data: function() {
    return {
      activeCardName: 'all',
      dialogFormVisible: false,
      dialogSee: false,
      dialogStatus: '',

      dateRange: null,
      langtopicTypeData: [],
      textMap: {
        update: '修改',
        create: '添加'
      },
      SeeData: {
        inputFinish: '',
        remarkSuiPian: '',
        remarkGoodRates: '',
        remarkOther: ''
      },
      temp: {
        costsDate: '',
        dayInput: null,
        dayStorage: null,
        lostMaterialsCost: null,
        depSalary: null,
        utilityBills: null,
        eqDepreciation: null,
        eqSpareBills: null,
        facSpareBills: null,
        laborBills: null,
        invitBills: null,
        dayTotalBills: null,
        singleCosts: null,
        inputCosts: null,
        remarks: null
      },
      rules: {
        costsDate: [{ required: true, message: '日期没有选择', trigger: 'change' }],
        dayInput: [{ required: true, message: '请输入每日投入', trigger: 'blur' }],
        dayStorage: [{ required: true, message: '请输入每日入库', trigger: 'blur' }],
        lostMaterialsCost: [{ required: true, message: '请输入损耗物料费用', trigger: 'blur' }],
        depSalary: [{ required: true, message: '请输入运营部工资', trigger: 'blur' }],
        utilityBills: [{ required: true, message: '请输入水电费用', trigger: 'blur' }],
        eqDepreciation: [{ required: true, message: '请输入设备折旧费用', trigger: 'blur' }],
        eqSpareBills: [{ required: true, message: '请输入设备备品备件费用', trigger: 'blur' }],
        facSpareBills: [{ required: true, message: '请输入厂务备品备件费用', trigger: 'blur' }],
        laborBills: [{ required: true, message: '请输入劳保五金费用', trigger: 'blur' }],
        invitBills: [{ required: true, message: '请输入招待费用', trigger: 'blur' }],
        dayTotalBills: [{ required: true, message: '请输入每日合计费用', trigger: 'blur' }],
        singleCosts: [{ required: true, message: '请输入单瓦成本', trigger: 'blur' }],
        inputCosts: [{ required: true, message: '请输入投入计算单瓦成本', trigger: 'blur' }],
        remarks: [{ required: true, message: '请输入备注', trigger: 'blur' }]
      },
      total: 0,
      pageNo: 1,
      pageSize: 10,
      list: [], // 全部数据
      listDataSelected: [] // 选中数据
    }
  },
  created() {
    // 获取数据
    this.fetchData()
  },
  methods: {
    // 重置数据
    resetTemp() {
      this.temp = {
        costsDate: '',
        dayInput: null,
        dayStorage: null,
        lostMaterialsCost: null,
        depSalary: null,
        utilityBills: null,
        eqDepreciation: null,
        eqSpareBills: null,
        facSpareBills: null,
        laborBills: null,
        invitBills: null,
        dayTotalBills: null,
        singleCosts: null,
        inputCosts: null,
        remarks: null
      }
    },
    // 查询所有数据
    async SeeDataQueryAllItems() {
      const params = {
        pageNum: 1,
        pageSize: 100
      }
      await queryAllItems(params).then(res => {
        this.langtopicTypeData = res.data.content
      })
    },
    // 计算
    // 打开添加早会弹窗
    handleCreate() {
      this.resetTemp()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    //  修改早会
    handleUpdate(row) {
      const data = Object.assign({}, row)
      this.temp = {
        costsDate: data.costsDate,
        dayInput: data.dayInput,
        dayStorage: data.dayStorage,
        lostMaterialsCost: data.lostMaterialsCost,
        depSalary: data.depSalary,
        utilityBills: data.utilityBills,
        eqDepreciation: data.eqDepreciation,
        eqSpareBills: data.eqSpareBills,
        facSpareBills: data.facSpareBills,
        laborBills: data.laborBills,
        invitBills: data.invitBills,
        dayTotalBills: data.dayTotalBills,
        singleCosts: data.singleCosts,
        inputCosts: data.inputCosts,
        remarks: data.remarks,
        costsId: data.costsId

      }
      console.log('temp', this.temp)
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    // 添加早会提交数据
    createData() {
      this.$refs['dataForm'].validate(async(valid) => {
        if (valid) {
          const data = {
            costsDate: this.temp.costsDate,
            dayInput: this.temp.dayInput,
            dayStorage: this.temp.dayStorage,
            lostMaterialsCost: this.temp.lostMaterialsCost,
            depSalary: this.temp.depSalary,
            utilityBills: this.temp.utilityBills,
            eqDepreciation: this.temp.eqDepreciation,
            eqSpareBills: this.temp.eqSpareBills,
            facSpareBills: this.temp.facSpareBills,
            laborBills: this.temp.laborBills,
            invitBills: this.temp.invitBills,
            dayTotalBills: this.temp.dayTotalBills,
            singleCosts: this.temp.singleCosts,
            inputCosts: this.temp.inputCosts,
            remarks: this.temp.remarks
          }
          console.log('data', data)
          await costsAdd(data).then((r) => {
            console.log('temp', this.temp)
            this.dialogFormVisible = false
            if (r && r.code == 200) {
              this.$message({
                type: 'success',
                message: r.message
              })
              this.fetchData()
            } else {
              this.$message({
                type: 'error',
                message: r.message
              })
            }
          })
        }
      })
    },
    // 修改早会提交数据
    updateData() {
      this.$refs['dataForm'].validate(async(valid) => {
        if (valid) {
          const date = this.getTime(this.temp.costsDate)

          const data = {
            costsDate: date,
            dayInput: this.temp.dayInput,
            dayStorage: this.temp.dayStorage,
            lostMaterialsCost: this.temp.lostMaterialsCost,
            depSalary: this.temp.depSalary,
            utilityBills: this.temp.utilityBills,
            eqDepreciation: this.temp.eqDepreciation,
            eqSpareBills: this.temp.eqSpareBills,
            facSpareBills: this.temp.facSpareBills,
            laborBills: this.temp.laborBills,
            invitBills: this.temp.invitBills,
            dayTotalBills: this.temp.dayTotalBills,
            singleCosts: this.temp.singleCosts,
            inputCosts: this.temp.inputCosts,
            remarks: this.temp.remarks,
            costsId: this.temp.costsId
          }
          await costsUpdate(data).then((r) => {
            this.dialogFormVisible = false
            this.singleTime = this.temp.singleTime
            if (r && r.code == 200) {
              this.$message({
                type: 'success',
                message: r.message
              })
              this.fetchData()
            } else {
              this.$message({
                type: 'error',
                message: r.data
              })
            }
          })
        }
      })
    },
    // 删除早会
    async handleDelete(row) {
      await costsDelete({ id: row.costsId }).then((r) => {
        if (r && r.code == 200) {
          this.$message({
            type: 'success',
            message: r.message
          })
          this.fetchData()
        } else {
          this.$message({
            type: 'error',
            message: r.message
          })
        }
      })
    },
    //  查看账户
    SeeRow(data) {
      this.SeeData = {
        inputFinish: data.inputFinish,
        remarkSuiPian: data.remarkSuiPian,
        remarkGoodRates: data.remarkGoodRates,
        remarkOther: data.remarkOther
      }
      this.dialogSee = true
    },
    // 分页组件
    pageSizeChange(newSize) {
      this.pageSize = newSize
      this.pageNo = 1
      this.fetchData()
    },

    async fetchData(pageNo) {
      if (pageNo) {
        this.pageNo = pageNo
      }
      const params = {
        pageNum: this.pageNo,
        pageSize: this.pageSize,

        starTime: '',
        endTime: ''
      }
      if (this.dateRange) {
        const start = this.getTime(this.dateRange[0])
        const end = this.getTime(this.dateRange[1])
        params.starTime = start
        params.endTime = end
      }
      const data = await costsAllItems(params)
      this.list = data.data.content
      this.total = data.data.totalSize
    },
    // 表格多选
    handleSelectionChange(val) {
      this.listDataSelected.splice(0)
      val.forEach(e => {
        this.listDataSelected.push(e)
      })
    },
    // 输入合计
    inputChange() {
      this.total = new BigNumber(this.temp.dayInput).plus(this.temp.dayStorage).plus(this.temp.lostMaterialsCost).plus(this.temp.depSalary).plus(this.temp.utilityBills).plus(this.temp.eqDepreciation).plus(this.temp.eqSpareBills).plus(this.temp.facSpareBills).plus(this.temp.laborBills).plus(this.temp.invitBills).plus(this.temp.dayTotalBills).plus(this.temp.singleCosts).plus(this.temp.inputCosts).toNumber()
      console.log('total', this.total)
    },
    // 时间戳转日期
    getTime(time) {
      var date = new Date(time)
      var y = date.getFullYear()
      var m = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1)
      var d = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate())
      return y + '-' + m + '-' + d
    }
  }
}
</script>
<style lang="scss" scoped>
.userlist {
    margin-right: 10px;
}

.title {
    height: 20px;
    padding-left: 5px;
    line-height: 20px;
    font-size: 20px;
    font-weight: bold;
    border-left: 7px solid #1587F9;
    margin-bottom: 23px;
    display: inline-block;
}
</style>
